package com.futurelight.recipes.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.GridLayoutManager;

import com.futurelight.recipes.R;
import com.futurelight.recipes.adapter.CallBackResult;
import com.futurelight.recipes.adapter.ShimmerAdapter;
import com.futurelight.recipes.model.Common;
import com.futurelight.recipes.model.ItemViewModel;
import com.futurelight.recipes.model.Result;
import com.futurelight.recipes.ui.activity.DetailActivity;
import com.todkars.shimmer.ShimmerRecyclerView;

import java.util.List;

public class RecipesInfoItemFragment extends Fragment {
    private int position;
    private ItemViewModel viewModel;

    View rootView;

    private RecipesInfoItemFragment(FragmentActivity activity, int position) {
        this.viewModel = new ItemViewModel(activity.getApplication());
        this.position = position;
    }
    private static RecipesInfoItemFragment instance;
    public static RecipesInfoItemFragment getInstance(FragmentActivity activity, int position) {
        if (instance == null){
            synchronized (RecipesInfoItemFragment.class){
                if (instance == null){
                    return new RecipesInfoItemFragment(activity,position);
                }
            }
        }
        return instance;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_recipes_info, container, false);
        return rootView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        ShimmerRecyclerView shimmerRecyclerView = rootView.findViewById(R.id.food_recipes_recycler_view);
        ShimmerAdapter shimmerAdapter = new ShimmerAdapter();
        shimmerRecyclerView.setAdapter(shimmerAdapter);

        shimmerRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));

        viewModel.getData().observe(getViewLifecycleOwner(), new Observer<Common>() {
            @Override
            public void onChanged(Common common) {
                if (common instanceof Common.Success) {
                    Common.Success<List<Result>> success = (Common.Success<List<Result>>) common;
                    List<Result> results = success.getData();

                    shimmerAdapter.setResults(results);
                    shimmerAdapter.setCallback(new CallBackResult() {
                        @Override
                        public void callBackResult(Result result) {
                            Intent intent = new Intent(requireActivity(), DetailActivity.class);
                            intent.putExtra("result", result);
                            startActivity(intent);
                            requireActivity().overridePendingTransition(R.anim.anim_enter, R.anim.anim_exit);
                        }

                        @Override
                        public void deleteCallBack(Result result) {

                        }

                        @Override
                        public void callText(String s) {

                        }
                    });

                    shimmerRecyclerView.hideShimmer();
                } else if (common instanceof Common.Loading) {
                    shimmerRecyclerView.showShimmer();
                } else {
                    Common.Error error = (Common.Error) common;
                    Toast.makeText(requireActivity(), error.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }
        });
        viewModel.fetchRecipes(ItemViewModel.types.get(position));
    }

}
